package org.finesys.ai.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.finesys.ai.constants.AigcControllerRestApiConfig;
import org.finesys.ai.entity.AigcConversation;
import org.finesys.ai.service.AigcConversationService;
import org.finesys.ai.vo.AigcConversationExcelVO;
import org.finesys.common.core.constants.RestApiConfig;
import org.finesys.common.core.constants.ValidationGroup;
import org.finesys.common.core.module.R;
import org.finesys.common.core.util.ValidationUtil;
import org.finesys.common.excel.annotaion.ResponseExcel;
import org.finesys.common.log.core.annotation.LogAnnotation;
import org.finesys.common.log.core.enums.OperateType;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Tag(name = "Aigc会话管理")
@RestController
@RequestMapping(value = AigcControllerRestApiConfig.AIGC_CONVERSATION)
@RequiredArgsConstructor
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class AigcConversationController {

    private final AigcConversationService aigcConversationService;


    /**
     * 添加
     */
    @Operation(summary = "添加")
    @LogAnnotation(title = "Aigc会话管理", operateType = OperateType.INSERT)
    @PostMapping(value = RestApiConfig.ADD)
    @PreAuthorize("@pms.hasPermission('aigc_conversation_add')")
    public R<Void> add(@RequestBody AigcConversation aigcConversation) {
        ValidationUtil.validateParam(aigcConversation, ValidationGroup.add.class);
        return R.judge(aigcConversationService.save(aigcConversation));
    }

    /**
     * 修改
     */
    @Operation(summary = "修改")
    @LogAnnotation(title = "Aigc会话管理", operateType = OperateType.UPDATE)
    @PreAuthorize("@pms.hasPermission('aigc_conversation_edit')")
    @PutMapping(value = RestApiConfig.UPDATE)
    public R<Void> update(@RequestBody AigcConversation aigcConversation) {
        ValidationUtil.validateParam(aigcConversation, ValidationGroup.update.class);
        return R.judge(aigcConversationService.updateById(aigcConversation));
    }

    /**
     * 删除
     */
    @Operation(summary = "删除")
    @DeleteMapping(value = RestApiConfig.DELETE)
    @PreAuthorize("@pms.hasPermission('aigc_conversation_delete')")
    @LogAnnotation(title = "Aigc会话管理", operateType = OperateType.DELETE)
    public R<Boolean> delete(@RequestBody String[] ids) {
        return R.judge(aigcConversationService.removeBatchByIds(Arrays.asList(ids)));
    }

    /**
     * 获取单条
     */
    @Operation(summary = "获取单条")
    @GetMapping(value = RestApiConfig.GET + "/{id}")
    public R<AigcConversation> findById(@PathVariable String id) {
        return R.success(aigcConversationService.getById(id));
    }

    /**
     * 获取全部
     */
    @Operation(summary = "获取全部")
    @GetMapping(value = RestApiConfig.ALL)
    public R<List<AigcConversation>> findAll() {
        List<AigcConversation> list = aigcConversationService.findAll();
        return R.success(list);
    }

    /**
     * 获取集合
     */
    @Operation(summary = "获取集合")
    @PostMapping(value = RestApiConfig.LIST)
    public R<List<AigcConversation>> findList(AigcConversation aigcConversation) {
        List<AigcConversation> list = aigcConversationService.findList(aigcConversation);
        return R.success(list);
    }

    /**
     * 分页查询
     *
     * @param page 分页对象
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping(RestApiConfig.PAGE)
    public R<?> page(Page<AigcConversation> page, AigcConversation aigcConversation) {
        return R.success(aigcConversationService.findPage(page, aigcConversation));
    }

    /**
     * 导出excel 表格
     *
     * @return
     */
    @ResponseExcel
    @GetMapping(RestApiConfig.EXPORT_EXCEL)
    @PreAuthorize("@pms.hasPermission('aigc_conversation_export')")
    public List<AigcConversationExcelVO> exportExcel() {
        return aigcConversationService.listExcel();
    }
}
